<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog"
      :custom-class="'panna-dialog_medium'"
      title="EntrustOrderPrice(委托单价格表)信息"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <el-form ref="formData" :model="formData" :rules="formDataRules" status-icon>
        <el-form-item
          label="委托单Id"
          :label-width="formLabelWidth"
          prop="entrustOrderId"
          :error="errorMsg.entrustOrderId"
        >
          <el-input v-model="formData.entrustOrderId" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="客户ID"
          :label-width="formLabelWidth"
          prop="customerId"
          :error="errorMsg.customerId"
        >
          <el-input v-model="formData.customerId" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="检测费用(检测项目价格+检测服务价格),这是原价,以分为单位"
          :label-width="formLabelWidth"
          prop="detectionPrice"
          :error="errorMsg.detectionPrice"
        >
          <el-input v-model="formData.detectionPrice" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="折扣方式选项1.检测项目协议价,2.整单折扣"
          :label-width="formLabelWidth"
          prop="discountTypeCode"
          :error="errorMsg.discountTypeCode"
        >
          <el-input v-model="formData.discountTypeCode" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="协议价,以分为单位"
          :label-width="formLabelWidth"
          prop="agreementPrice"
          :error="errorMsg.agreementPrice"
        >
          <el-input v-model="formData.agreementPrice" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="整单折扣,以分为单位"
          :label-width="formLabelWidth"
          prop="discount"
          :error="errorMsg.discount"
        >
          <el-input v-model="formData.discount" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="优惠价(检测费用选择协议价之后生成优惠价格,以分为单位)"
          :label-width="formLabelWidth"
          prop="favorablePrice"
          :error="errorMsg.favorablePrice"
        >
          <el-input v-model="formData.favorablePrice" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="最终价(检测费用-优惠价,以分为单位)"
          :label-width="formLabelWidth"
          prop="finalPrice"
          :error="errorMsg.finalPrice"
        >
          <el-input v-model="formData.finalPrice" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="价格状态(0未核算,1已核算,2核算中),驳回之后进入历史记录表,状态初始化为0"
          :label-width="formLabelWidth"
          prop="pricelStatusCode"
          :error="errorMsg.pricelStatusCode"
        >
          <el-input v-model="formData.pricelStatusCode" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="审核流Id"
          :label-width="formLabelWidth"
          prop="workFlowId"
          :error="errorMsg.workFlowId"
        >
          <el-input v-model="formData.workFlowId" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item
          label="审核状态,依据审批流返回"
          :label-width="formLabelWidth"
          prop="approvalStatusCode"
          :error="errorMsg.approvalStatusCode"
        >
          <el-input v-model="formData.approvalStatusCode" autocomplete="off" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "../../../api/EntrustManage/EntrustOrderPrice.js";

export default {
  name: "EntrustOrderPriceEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        entrustOrderId: 0,
        customerId: 0,
        detectionPrice: 0,
        discountTypeCode: 0,
        agreementPrice: 0,
        discount: 0,
        favorablePrice: 0,
        finalPrice: 0,
        pricelStatusCode: 0,
        workFlowId: 0,
        approvalStatusCode: 0,
      },
      formData: {
        id: 0,
        entrustOrderId: 0,
        customerId: 0,
        detectionPrice: 0,
        discountTypeCode: 0,
        agreementPrice: 0,
        discount: 0,
        favorablePrice: 0,
        finalPrice: 0,
        pricelStatusCode: 0,
        workFlowId: 0,
        approvalStatusCode: 0,
      },
      formDataRules: {
        entrustOrderId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        customerId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        detectionPrice: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        discountTypeCode: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        agreementPrice: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        discount: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        favorablePrice: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        finalPrice: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        pricelStatusCode: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        workFlowId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        approvalStatusCode: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
      },
      initErrorMsg: {
        entrustOrderId: "",
        customerId: "",
        detectionPrice: "",
        discountTypeCode: "",
        agreementPrice: "",
        discount: "",
        favorablePrice: "",
        finalPrice: "",
        pricelStatusCode: "",
        workFlowId: "",
        approvalStatusCode: "",
      },
      errorMsg: {
        entrustOrderId: "",
        customerId: "",
        detectionPrice: "",
        discountTypeCode: "",
        agreementPrice: "",
        discount: "",
        favorablePrice: "",
        finalPrice: "",
        pricelStatusCode: "",
        workFlowId: "",
        approvalStatusCode: "",
      },
    };
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("entrustOrderId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("customerId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("detectionPrice", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("discountTypeCode", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("agreementPrice", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("discount", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("favorablePrice", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("finalPrice", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("pricelStatusCode", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("workFlowId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("approvalStatusCode", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
